package com.region.http.client.core;

import com.region.http.client.annotation.configurer.ParameterConstant;
import com.region.http.client.model.RequestMeta;
import com.region.http.client.model.RequestMeta.RequestMetaBuilder;
import com.region.http.client.model.RequestMeta.ConstParameter.ConstParameterBuilder;

import java.lang.annotation.Annotation;

/**
 * @author 26225
 * @date 2024/8/31 16:28
 * @description
 */
public class ConstMetaResolver implements MetaResolver {
    @Override
    public void doResolver(RequestMetaBuilder builder, Annotation annotation, MetaChain chain) {
        if (annotation instanceof ParameterConstant) {
            ParameterConstant constant = (ParameterConstant) annotation;
            ConstParameterBuilder constParameterBuilder = RequestMeta.ConstParameter.builder();
            if (constant.headers().length != 0) {
                for (ParameterConstant.ParameterConstantEntry header : constant.headers()) {
                    constParameterBuilder.addHeaderConst(header.key(), header.value());
                }
            }
            if (constant.pathVariables().length != 0) {
                for (ParameterConstant.ParameterConstantEntry pathVariable : constant.pathVariables()) {
                    constParameterBuilder.addPathConst(pathVariable.key(), pathVariable.value());
                }
            }
            if (constant.params().length != 0) {
                for (ParameterConstant.ParameterConstantEntry param : constant.params()) {
                    constParameterBuilder.addParameterConst(param.key(), param.value());
                }
            }
            if (constant.body() != null && !"".equals(constant.body())) {
                constParameterBuilder.addBodyConst(constant.body());
            }
            builder.constParameter(constParameterBuilder.build());
        }
        chain.doResolver(builder, annotation);
    }
}
